//
//  UpdateCarTableViewController.m
//  CarTown
//
//  Created by xionghao on 15/9/15.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "UpdateCarTableViewController.h"
#import "PickerTableViewCell.h"
#import "selectView.h"
#import "PikerImageTableViewCell.h"
#import "CarpubMode.h"
#import "Pubmode.h"
#import "IWUploadParam.h"
#import "EidtTableViewCell.h"
#import "PublishTopTableViewCell.h"
#import "RadioTableViewCell.h"
#import "NoramlCellMode.h"
#import "PickViewPhotoTableViewCell.h"
#import "PickerTableViewController.h"
#import  <AssetsLibrary/AssetsLibrary.h>
#import <QBImagePickerController/QBImagePickerController.h>
#import "CarbrandModel.h"
#import "CarprogChildmode.h"
#import "ColorPickViewController.h"
#import "FindCarTableViewController.h"
#import "CarsrouceTypePickerController.h"
#import "CarDetail.h"
#import "ListPikerController.h"
#import "Position.h"
#import "Area.h"
#import "Process.h"
#import "JianjieViewController.h"
#import "CarPickModel.h"
#import "CarSourceType.h"
#import "Puser.h"
#import "PostSUSViewController.h"
#import "CarDetailTableViewCell.h"
#import "PostResdemode.h"
#import "IndexCarModel.h"
#import "CarSourceType.h"
@interface UpdateCarTableViewController ()<ImagePickerBtnDelegate,QBImagePickerControllerDelegate,PickerViewCellDelegate,ColorPikerDelegate,ListPickerDelegate,PostSUSViewControllerDelegate>
@property (nonatomic,strong) UIImagePickerController *picker;
@property (nonatomic,strong) NSMutableArray *array;
@property (nonatomic,strong) NSMutableArray *setetionarray;
@property (nonatomic,strong) PickViewPhotoTableViewCell *currtcell;
@property (nonatomic,strong) UIButton *button;
@property (nonatomic,strong)  Pubmode *leixing;
@property (nonatomic,assign)  NSInteger currpostion;
@property (nonatomic,copy)  NSString *csid;
@property (nonatomic,strong) CarpubMode *youhui;
@property (nonatomic,strong) Pubmode *baojia;
@property (nonatomic,strong) Pubmode *peizhi;
@property (nonatomic,strong) Pubmode *waiguan;
@property (nonatomic,strong) Pubmode *neishi;
@property (nonatomic,strong) CarPickModel *chexing;
@property (nonatomic,strong) Pubmode *kuwei;
@property (nonatomic,strong) Pubmode *quyu;
@property (nonatomic,strong) Pubmode *shouxu;
@property (nonatomic,strong) Pubmode *peizhijia;
@property (nonatomic,strong) Pubmode *yikoujia;
@property (nonatomic,strong) Pubmode *xingzhi;
@property (nonatomic,strong) Pubmode *paifang;
@property (nonatomic,strong) Pubmode *daiqi;
@property (nonatomic,strong) Pubmode *chejiahao;
@property (nonatomic,strong) Pubmode *zeprice;
@property (nonatomic,strong) Pubmode *zebili;
@property (nonatomic,strong) CarpubMode *carimg;
@property (nonatomic,strong) CarpubMode *carinofimg;
@property (nonatomic,strong) NSArray *kuweiarray;
@property (nonatomic,strong) NSArray *quyuarray;
@property (nonatomic,strong) NSArray *shouxuarray;
@property (nonatomic,strong) Puser *user;
@property (nonatomic,strong) CarDetailmodel *cardetailmode;
@end

@implementation UpdateCarTableViewController

+(instancetype)viewcontroller{
    UpdateCarTableViewController *vc=[[UpdateCarTableViewController alloc]initWithStyle:UITableViewStyleGrouped];
    return vc;
}
-(void)clickWithCellTag:(PickerTableViewCell *)cell index:(NSInteger)index{
    self.youhui.type=index;
}
-(Puser *)user{
    if (_user==nil) {
        _user=[Puser getUser];
    }
    return _user;
}

-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    self.tableView.backgroundColor=bgGarycolor;
    self.navigationItem.title=@"修改车源信息";
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getcarname:) name:NoticePostGetCar object:nil];
    [self stuppostbtn];
    
    
    [self getCarDetailMode];
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
#pragma -mark 车型选择回调
-(void)getcarname:(NSNotification *)notice{
    CarDetail *detail=notice.userInfo[@"cardetail"];
    CarbrandModel *brand=notice.userInfo[@"brand"];
    CarprogChildmode *carprogchild=notice.userInfo[@"cartory"];
    self.chexing.detail=detail.name;
    self.chexing.cardetail=detail;
    self.chexing.carporgchild=carprogchild;
    self.chexing.brand=brand;
    
    NSIndexPath *indexpath=[NSIndexPath indexPathForRow:1 inSection:0];
    
    [self.tableView reloadRowsAtIndexPaths:@[indexpath] withRowAnimation:UITableViewRowAnimationAutomatic];
    
}
-(void)getCarDetailMode{
    NSMutableDictionary *params=[NSMutableDictionary dictionary];
    params[@"action"]=@"where";
    params[@"is_sale"]=@"1";
    params[@"cond"]=[NSString stringWithFormat:@"id=%@",_ID];
    [PostUitls Post:carurl parameters:params success:^(id response) {
        NSDictionary *dict=response;
        IndexCarModel *resmode=[IndexCarModel indexcarWithDcit:dict];
        self.cardetailmode=resmode.data[0];
        [self setupdata];
        [self.tableView reloadData];
        NSLog(@"%@",response);
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
#pragma -mark 初始化发送按钮
/**
 *  初始化发送按钮
 */
-(void)stuppostbtn{
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, XHScreenwidth, 54)];
    view.backgroundColor=[UIColor clearColor];
    UIButton *postbtn=[[UIButton alloc]initWithFrame:CGRectMake(16, 8, XHScreenwidth-32, 36)];
    [postbtn addTarget:self action:@selector(postclick) forControlEvents:UIControlEventTouchUpInside];
    [postbtn setBackgroundImage:[UIImage imageWithStretchableName:@"button_f"] forState:UIControlStateNormal];
    [postbtn setTitle:@"重新发布" forState:UIControlStateNormal];
    [postbtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [view addSubview:postbtn];
    self.tableView.tableFooterView=view;
}
-(void)clearother{
    self.chexing.cardetail=nil;
    self.chexing.brand=nil;
    self.chexing.carporgchild=nil;
    self.chexing.detail=@"请选择";
    
    self.neishi.detail=@"请选择";
    self.waiguan.detail=@"请选择";
}

-(NSArray *)kuweiarray{
    if (_kuweiarray==nil) {
        NSMutableArray *marray=[NSMutableArray array];
        NSArray *array=[Position getPostion];
        for (Position *postion in array) {
            [marray addObject:postion.name];
        }
        _kuweiarray=marray;
    }
    return _kuweiarray;
}
-(NSArray *)quyuarray{
    if (_quyuarray==nil) {
        NSMutableArray *marray=[NSMutableArray array];
        NSArray *array=[Area getArea];
        for (Area *ar in array) {
            [marray addObject:ar.name];
        }
        _quyuarray=marray;
    }
    return _quyuarray;
}
-(NSArray *)shouxuarray{
    if (_shouxuarray==nil) {
        NSMutableArray *marray=[NSMutableArray array];
        NSArray *array=[Process getProcess];
        for (Process *pr in array) {
            [marray addObject:pr.p_name];
        }
        _shouxuarray=marray;
    }
    return _shouxuarray;
}



#pragma -mark设置数据模型
-(void)setupdata{
    
    Pubmode *leixing=[[Pubmode alloc]init];
    leixing.name=@"车源类型";
    leixing.isMust=YES;
    leixing.detail=
    self.cardetailmode.car_source_type;
    CarSourceType *type= [CarSourceType getcarsoucebyText:leixing.detail];
    self.csid=type.csid;
    leixing.block=^{
        CarsrouceTypePickerController *picker=[CarsrouceTypePickerController picker];
        picker.currtpostion=self.currpostion;
        picker.title=leixing.name;
        picker.block=^(NSString *str,NSString *cs_id,NSInteger currtpostion){
            self.leixing.detail=str;
            self.currpostion=currtpostion;
            self.csid=cs_id;
            [self clearother];
            if ([str isEqualToString:@"中规"]) {
                if ([self.setetionarray containsObject:self.youhui]) {
                    return ;
                }
                [self.setetionarray insertObject:self.youhui atIndex:4];
                self.baojia.placehoder=@"自动生成";
                self.baojia.isAluto=YES;
                self.baojia.yuancolor=tintblue;
            }else{
                
                [self.setetionarray removeObject:self.youhui];
                self.baojia.isAluto=NO;
                self.baojia.yuancolor=textnormalGary;
                self.baojia.placehoder=@"请输入";
            }
            
            NSIndexSet *set=[NSIndexSet indexSetWithIndex:0];
            
            [self.tableView reloadSections:set withRowAnimation:UITableViewRowAnimationAutomatic];
        };
        
        [self.navigationController pushViewController:picker animated:YES];
    };
    self.leixing=leixing;
    CarPickModel *chexing=[[CarPickModel alloc]init];
    chexing.name=@"车型";
    chexing.isMust=YES;
    chexing.detail=self.cardetailmode.car_name;
    CarDetail *cardetail=[[CarDetail alloc]init];
    cardetail.ID=self.cardetailmode.mid;
    cardetail.name=self.cardetailmode.car_name;
    cardetail.price=self.cardetailmode.sprice;
    cardetail.is_conf=![self.cardetailmode.conf_price isEqualToString:@""]?@"0":@"1";
    chexing.cardetail=cardetail;
    chexing.block=^{
        if ([self.leixing.detail isEqualToString:@"请选择"]) {
            MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
            hud.labelText=@"请先选择车源类型";
            hud.mode = MBProgressHUDModeText;
            hud.margin = 10.f;
            hud.removeFromSuperViewOnHide = YES;
            [hud hide:YES afterDelay:1];
        }else{
            FindCarTableViewController *fincar=[FindCarTableViewController findcar];
            fincar.type=self.csid;
            [self.navigationController pushViewController:fincar animated:YES];
        }
    };
    self.chexing=chexing;
    Pubmode *waiguan=[[Pubmode alloc]init];
    waiguan.name=@"外观颜色";
    waiguan.isMust=YES;
   
    waiguan.block=^{
        
        if ([self.chexing.detail isEqualToString:@"请选择"]) {
            MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
            hud.labelText=@"请先选择车型";
            hud.mode = MBProgressHUDModeText;
            hud.margin = 10.f;
            hud.removeFromSuperViewOnHide = YES;
            [hud hide:YES afterDelay:1];
        }else{
            ColorPickViewController *color=[ColorPickViewController colorpicker];
            color.delegate=self;
            
            color.ID=self.chexing.cardetail.ID;
            color.index=self.waiguan.index;
            color.colorstr=self.waiguan.detail;
            color.isOut=YES;
            color.view.tag=0;
            [self.navigationController pushViewController:color animated:YES];}
    };
    self.waiguan=waiguan;
    Pubmode *neishi=[[Pubmode alloc]init];
    neishi.name=@"内饰颜色";
    neishi.isMust=YES;
    neishi.detail=![self.cardetailmode.car_inside_color isEqualToString:@""]?self.cardetailmode.car_inside_color:@"请选择";
;
    self.neishi=neishi;
    neishi.block=^{
        
        if ([self.chexing.detail isEqualToString:@"请选择"]) {
            MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
            hud.labelText=@"请先选择车型";
            hud.mode = MBProgressHUDModeText;
            hud.margin = 10.f;
            
            hud.removeFromSuperViewOnHide = YES;
            [hud hide:YES afterDelay:1];
        }else{
            ColorPickViewController *color=[ColorPickViewController colorpicker];
            color.delegate=self;
            
            color.ID=self.chexing.cardetail.ID;
            color.index=self.neishi.index;
            color.colorstr=self.waiguan.detail;
            color.isOut=NO;
            color.view.tag=1;            [self.navigationController pushViewController:color animated:YES];}
        
    };
    Pubmode *baojia=[[Pubmode alloc]init];
    baojia.name=@"报价";
    baojia.isMust=YES;
    baojia.isEidt=YES;
    baojia.pricetitle=@"万";
    self.baojia=baojia;
    CarpubMode *youhui=[[CarpubMode alloc]init];
    youhui.name=@"优惠";
    youhui.isSelect=YES;
    youhui.type=YOUHUIPOINT;
    self.youhui=youhui;
    Pubmode *peizhijia=[[Pubmode alloc]init];
    peizhijia.name=@"配置价";
    peizhijia.isMust=YES;
    peizhijia.isEidt=YES;
    
    peizhijia.pricetitle=@"万";
    self.peizhijia=peizhijia;
    
    Pubmode *yikoujia=[[Pubmode alloc]init];
    yikoujia.name=@"是否一口价";
    yikoujia.isMust=YES;
    
    yikoujia.titlearray=@[@"一口价",@"可议价"];
    yikoujia.radioindex=0;
    
    [self.setetionarray addObject:leixing];
    [self.setetionarray addObject:chexing];
    [self.setetionarray addObject:waiguan];
    [self.setetionarray addObject:neishi];
    [self.setetionarray addObject:baojia];
    [self.setetionarray addObject:yikoujia];
    
    
    Pubmode *cheyuanxingzhi=[[Pubmode alloc]init];
    cheyuanxingzhi.name=@"车源性质";
    cheyuanxingzhi.isMust=NO;
    cheyuanxingzhi.titlearray=@[@"自店",@"资源"];
    self.xingzhi=cheyuanxingzhi;
    switch ([self.cardetailmode.car_nature integerValue]) {
        case 0:
            cheyuanxingzhi.radioindex=0;
            break;
        case 1:
            cheyuanxingzhi.radioindex=1;
            break;
            
        default:
            cheyuanxingzhi.radioindex=-1;
            break;
    }
    
    Pubmode *paifang=[[Pubmode alloc]init];
    paifang.name=@"排放标准";
    paifang.isMust=NO;
    paifang.titlearray=@[@"国四",@"国五",@"不限"];
    switch ([self.cardetailmode.standard integerValue]) {
        case 4:
            paifang.radioindex=0;
            break;
        case 5:
            paifang.radioindex=1;
            break;
        case 0:
            paifang.radioindex=2;
            break;
        default:
            paifang.radioindex=-1;
            break;
    }

    self.paifang=paifang;
    Pubmode *qidai=[[Pubmode alloc]init];
    qidai.name=@"货期";
    qidai.isMust=NO;
    qidai.titlearray=@[@"期货",@"现货"];
    switch ([self.cardetailmode.timer integerValue]) {
        case 0:
            qidai.radioindex=0;
            break;
        case 1:
            qidai.radioindex=1;
            break;
            
        default:
            qidai.radioindex=-1;
            break;
    }

    self.daiqi=qidai;
    Pubmode *chejiahao=[[Pubmode alloc]init];
    chejiahao.name=@"车架号";
    chejiahao.isEidt=YES;
    chejiahao.placehoder=@"请输入车架号";
    chejiahao.pricetitle=self.cardetailmode.car_number;
    
    self.chejiahao=chejiahao;
    Pubmode *kuwei=[[Pubmode alloc]init];
    kuwei.name=@"库位";
    kuwei.detail=self.cardetailmode.stock_position;
    kuwei.block=^{
        [self showpicker:0];
    };
    
    self.kuwei=kuwei;
    
    Pubmode *quyu=[[Pubmode alloc]init];
    quyu.name=@"销售区域";
      quyu.detail=self.cardetailmode.sale_area;
    quyu.block=^{
        [self showpicker:1];
    };
    self.quyu=quyu;
    Pubmode *shouxu=[[Pubmode alloc]init];
    shouxu.name=@"手续";
     shouxu.detail=self.cardetailmode.fee;
    shouxu.block=^{
        [self showpicker:2];
    };
    self.shouxu=shouxu;
    NSArray *array=@[cheyuanxingzhi,paifang,qidai,chejiahao,kuwei,quyu,shouxu];
    
    Pubmode *peizhi=[[Pubmode alloc]init];
    peizhi.name=@"配置";
    peizhi.detail=self.cardetailmode.car_configure;
    self.peizhi=peizhi;
    peizhi.block=^{
        JianjieViewController *jjvc=[JianjieViewController viewcontroller];
        jjvc.navtitle=@"配置";
        jjvc.textlength=500;
        jjvc.text=self.peizhi.detail;
        jjvc.block=^(NSString *str){
            self.peizhi.detail=str;
            NSIndexPath *indexpath=[NSIndexPath indexPathForRow:0 inSection:2];
            [self.tableView reloadRowsAtIndexPaths:@[indexpath] withRowAnimation:UITableViewRowAnimationAutomatic];
            
        };
        [self.navigationController pushViewController:jjvc animated:YES];
    };
    CarpubMode *carmode=[[CarpubMode alloc]init];
    carmode.name=@"车辆图片";
    carmode.isPhotoCell=YES;
    carmode.imgarray=[NSMutableArray array];
    carmode.urlarray=self.cardetailmode.imgs;
    self.carimg=carmode;
    CarpubMode *carmode2=[[CarpubMode alloc]init];
    carmode2.name=@"车辆资料";
    carmode2.isPhotoCell=YES;
    carmode2.imgarray=[NSMutableArray array];
    self.carinofimg=carmode2;
    [self.array addObject:self.setetionarray];
    [self.array addObject:array];
    [self.array addObject:@[peizhi]];
    [self.array addObject:@[carmode,carmode2]];
    
}

#pragma -mark 选择控制器的回调方法

-(void)ListPickerDelegate:(ListPikerController *)picker WithIndex:(NSInteger)index{
    
    switch (picker.view.tag) {
        case 0:
        {
            self.kuwei.detail=self.kuweiarray[index];
            self.kuwei.index=index;
            NSIndexPath *indexpath=[NSIndexPath indexPathForRow:0 inSection:1];
            [self.tableView reloadRowsAtIndexPaths:@[indexpath] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
            break;
        case 1:
        {
            self.quyu.detail=self.quyuarray[index];
            self.quyu.index=index;
            NSIndexPath *indexpath=[NSIndexPath indexPathForRow:2 inSection:1];
            [self.tableView reloadRowsAtIndexPaths:@[indexpath] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
            break;
        case 2:
        {
            self.shouxu.detail=self.shouxuarray[index];
            self.shouxu.index=index;
            NSIndexPath *indexpath=[NSIndexPath indexPathForRow:3 inSection:1];
            [self.tableView reloadRowsAtIndexPaths:@[indexpath] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
            break;
            
        default:
            break;
    }
}

-(void)ListPickerDelegate:(ListPikerController *)picker WithCumerString:(NSString *)string{
    self.quyu.detail=string;
    NSIndexPath *indexpath=[NSIndexPath indexPathForRow:2 inSection:1];
    [self.tableView reloadRowsAtIndexPaths:@[indexpath] withRowAnimation:UITableViewRowAnimationAutomatic];
    
}
#pragma -mark 颜色选择回调
-(void)ColorPikcerClickContoller:(ColorPickViewController *)picker withIndex:(NSInteger)index ColorStr:(NSString *)colorstr{
    NSIndexPath *indexpath;
    if(picker.view.tag==0){
        self.waiguan.detail=colorstr;
        self.waiguan.index=index;
        indexpath=[NSIndexPath indexPathForRow:3 inSection:0];
        
    }else{
        self.neishi.detail=colorstr;
        self.neishi.index=index;
        indexpath=[NSIndexPath indexPathForRow:4 inSection:0];
    }
    [self.tableView reloadRowsAtIndexPaths:@[indexpath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self.tableView endEditing:YES];
}
#pragma -mark 显示选择控制器
-(void)showpicker:(NSInteger)tag{
    ListPikerController *picker=[ListPikerController viewcontroller];
    picker.view.tag=tag;
    picker.delegate=self;
    switch (tag) {
        case 0:
            picker.currtpostion=self.kuwei.index;
            picker.dataarray=self.kuweiarray;
            picker.navigationItem.title=@"库位";
            break;
        case 1:
            picker.currtpostion=self.quyu.index;
            picker.iscanEidt=YES;
            picker.dataarray=self.quyuarray;
            picker.navigationItem.title=@"销售区域";
            break;
        case 2:
            picker.currtpostion=self.shouxu.index;
            picker.dataarray=self.shouxuarray;
            picker.navigationItem.title=@"手续";
            break;
        default:
            break;
    }
    [self.navigationController pushViewController:picker animated:YES];
    
}


-(void)postclick{
    if ([self checkfrom]) {
        
    }
}
-(BOOL)checkfrom{
    if ([self.leixing.detail isEqualToString:@"请选择"]) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"请选择类型";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return NO;
    }
    if ([self.chexing.detail isEqualToString:@"请选择"]) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"请选择车型";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return NO;
    }
    if ([self.waiguan.detail isEqualToString:@"请选择"]) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"请选择外观颜色";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return NO;
    }
    if ([self.neishi.detail isEqualToString:@"请选择"]) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"请选择内饰颜色";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return NO;
    }
    if ([self.neishi.detail isEqualToString:@"请选择"]) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"请选择内饰颜色";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return NO;
    }
    return YES;
}
-(void)photoCellView:(PickViewPhotoTableViewCell *)cell pickerBtnClick:(NSInteger)index withButton:(UIButton *)btn{
    self.button=btn;
    self.currtcell=cell;
    
    if (index==0) {
        UIImagePickerControllerSourceType sourcheType = UIImagePickerControllerSourceTypeCamera;
        self.picker.sourceType=sourcheType;
        [self presentViewController:self.picker animated:YES completion:nil];
    }else{
        
        
        NSIndexPath *indexpath=[self.tableView indexPathForCell:cell];
        NSArray *array=self.array[indexpath.section];
        CarpubMode *photomode=array[indexpath.row];
        
        
        QBImagePickerController *imagePickerController = [QBImagePickerController new];
        imagePickerController.delegate = self;
        imagePickerController.allowsMultipleSelection = YES;
        if (btn.isSelected) {
            imagePickerController.maximumNumberOfSelection = 1;
        }else{
            imagePickerController.maximumNumberOfSelection = 8-photomode.imgarray.count;
        }
        imagePickerController.showsNumberOfSelectedAssets = YES;
        
        
        [self presentViewController:imagePickerController animated:YES completion:NULL];
    }
}
-(UIImagePickerController *)picker{
    if (_picker==nil) {
        _picker=[[UIImagePickerController alloc]init];
        _picker.view.backgroundColor = [UIColor whiteColor];
        
        _picker.delegate =self;
        _picker.allowsEditing = YES;
    }
    return _picker;
}

-(void)qb_imagePickerController:(QBImagePickerController *)imagePickerController didSelectAssets:(NSArray *)assets{
    NSMutableArray *marray=[NSMutableArray array];
    
    [self dismissViewControllerAnimated:YES completion:^{
        for (ALAsset *asset in assets) {
            
            UIImage *image=[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]];
            [marray addObject:image];
            
        }
        NSIndexPath *indexpath=[self.tableView indexPathForCell:self.currtcell];
        NSArray *array=self.array[indexpath.section];
        CarpubMode *photomode=array[indexpath.row];
        if (self.button.isSelected) {
            [photomode.imgarray replaceObjectAtIndex:self.button.tag withObject:marray[0]];
        }else{
            
            [photomode.imgarray addObjectsFromArray:marray];
        }
        [self.tableView reloadRowsAtIndexPaths:@[indexpath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }];
    
}
-(void)photoCellViewReloadImage:(PickViewPhotoTableViewCell *)cell{
    NSIndexPath *indexpath=[self.tableView indexPathForCell:cell];
    
    [self.tableView reloadRowsAtIndexPaths:@[indexpath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
- (void)qb_imagePickerControllerDidCancel:(QBImagePickerController *)imagePickerController {
    [self dismissViewControllerAnimated:YES completion:NULL];
}
-(NSMutableArray *)array{
    if (_array==nil) {
        _array=[NSMutableArray array];
    }
    return _array;
}
-(NSMutableArray *)setetionarray{
    if (_setetionarray==nil) {
        _setetionarray=[NSMutableArray array];
    }
    return _setetionarray;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
    // Return the number of sections.
   return self.array.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    NSArray *array=self.array[section];
    return array.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *array=self.array[indexPath.section];
    Pubmode *mode=array[indexPath.row];
    if (mode.isSelect) {
        return 44;
    }else if ([mode isKindOfClass:[CarpubMode class]]) {
        CarpubMode *photo=mode;
        
        if (photo.imgarray.count>=4) {
            return 179;
        }else{
            return 111;
        }
    }else{
        return 44;
    }
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *array=self.array[indexPath.section];
    Pubmode *mode=array[indexPath.row];
    if (mode.titlearray) {
        RadioTableViewCell *cell=[RadioTableViewCell cellWithTablView:tableView];
        cell.mode=mode;
        cell.ismustlable.hidden=!mode.isMust;
        return cell;
    }else if (mode.isSelect){
        CarpubMode *cmode=mode;
        PickerTableViewCell *cell=[PickerTableViewCell pickercellWithTableview:tableView];
        cell.delegate=self;
        cell.mode=cmode;
        return cell;
    }
    else if (mode.isEidt){
        EidtTableViewCell *cell=[EidtTableViewCell cellWithTable:tableView];
        cell.mode=mode;
        cell.price.text=mode.pricetitle;
        return cell;
    }else if ([mode isKindOfClass:[CarpubMode class]]){
        CarpubMode *photomode=mode;
        
        PickViewPhotoTableViewCell *picker=[PickViewPhotoTableViewCell cellWithTablView:tableView];
        picker.delegate=self;
        picker.mode=photomode;
        return picker;
    }
    else if ([mode isKindOfClass:[CarPickModel class]]&&[self.leixing.detail isEqualToString:@"中规"]){
        CarDetailTableViewCell *cell=[CarDetailTableViewCell cellwithTablview:tableView];
        if (self.chexing.cardetail) {
            cell.carname.text=self.chexing.cardetail.name;
            cell.carname.textColor=textnormalGary;
            cell.price.text=[NSString stringWithFormat:@"%@",self.chexing.cardetail.price];
            cell.price.textColor=textnormalGary;
        }else{
            cell.carname.text=nil;
            cell.price.text=@"请选择";
            cell.price.textColor=placehodlerColor;
            cell.carname.textColor=placehodlerColor;
        }
        
        return cell;
    }
    
    else
    {
        PublishTopTableViewCell *cell=[PublishTopTableViewCell cellWithTableView:tableView];
        cell.titlelable.text=mode.name;
        cell.startlable.hidden=!mode.isMust;
        cell.detail.text=mode.detail;
        if ([mode.detail isEqualToString:@"请选择"]) {
            cell.detail.textColor=placehodlerColor;
        }else{
            cell.detail.textColor=textnormalGary;
        }
        return cell;
        
    }
}


-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    [self dismissViewControllerAnimated:YES completion:^{
        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
        
        NSIndexPath *indexpath=[self.tableView indexPathForCell:self.currtcell];
        NSArray *array=self.array[indexpath.section];
        CarpubMode *photomode=array[indexpath.row];
        if (self.button.isSelected) {
            [photomode.imgarray replaceObjectAtIndex:self.button.tag withObject:image];
        }else{
            [photomode.imgarray addObject:image];
        }
        
    }];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *array=self.array[indexPath.section];
    Pubmode *mode=array[indexPath.row];
    if (mode.block) {
        mode.block();
    }
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [self dismissViewControllerAnimated:YES completion:nil];
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
